home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SORTDEMO.ARJ / SDSORT01.INC < prev    next >
Text File  |  1992-04-15  |  2KB  |  62 lines

  1. (*
  2. ╔═══════════════════════════════════════════════════════════════════════════╗
  3. ║ Turbo Pascal 6.0 Include File : SDSORT01.INC                              ║
  4. ╟───────────────────────────────────────────────────────────────────────────╢
  5. ║ Program : SORTDEMO.PAS                                                    ║
  6. ╟───────────────────────────────────────────────────────────────────────────╢
  7. ║ Version : 1.0                                                             ║
  8. ╟───────────────────────────────────────────────────────────────────────────╢
  9. ║ Copyright (c) 1992  by  Jon S. Russell                                    ║
  10. ╟───────────────────────────────────────────────────────────────────────────╢
  11. ║ Bubble sort routine for SORTDEMO.PAS                                      ║
  12. ╚═══════════════════════════════════════════════════════════════════════════╝
  13.                                                                            *)
  14. procedure BubbleSort (var Info : InfoType);
  15. var
  16.   Current : IndexType;
  17.  
  18.   (*───────────────────────────────────────────────────────────────────────*)
  19.  
  20.   procedure BubbleUp (var Info       : InfoType;
  21.                           StartIndex : IndexType;
  22.                           EndIndex   : IndexType);
  23.   var
  24.     Index : IndexType;
  25.  
  26.   begin  (* BubbleUp *)
  27.  
  28.     (* Loop Invariant: StartIndex <= Index <= EndIndex AND *)
  29.     (* List[Index+1] .. List[EndIndex] >= List[Index].     *)
  30.  
  31.     for Index := EndIndex downto StartIndex+1 do
  32.       if (Info.List[Index].Key < Info.List[Index-1].Key)
  33.         then Swap(Info,Index,Index-1);
  34.   end;   (* BubbleUp *)
  35.  
  36.   (*───────────────────────────────────────────────────────────────────────*)
  37.  
  38. begin  (* BubbleSort *)
  39.   Current := 1;  (* index of first unsorted element *)
  40.  
  41.   (* Loop Invariant: 1 <= Current <= Info.Len AND    *)
  42.   (* the values in List[1] .. List[Current-1] are    *)
  43.   (* sorted and are less than or equal to the values *)
  44.   (* in List[Current] .. List[Len].                  *)
  45.  
  46.   while Current < Info.Len do
  47.     begin
  48.       (* Bubble up the smallest element from the unsorted *)
  49.       (* part of the list, with intermediate swaps.       *)
  50.  
  51.       BubbleUp(Info, Current, Info.Len);
  52.  
  53.       (* Shrink the unsorted part of the list *)
  54.  
  55.       Inc(Current);
  56.     end;  (* while *)
  57.  
  58.   Info.Sorted := true;  (* set flag *)
  59. end;   (* BubbleSort *)
  60.  
  61. (*─────────────────────────────────────────────────────────────────────────*)
  62.